//
//  ZSPageBarView.m
//  ZSComponent
//
//  Created by HuangZuShu on 17/9/7.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "ZSPageBarView.h"

static int TagAdditional = 100;
static NSString * kZSPageCollectionViewCellID = @"ZSPageCollectionViewCell";


@interface ZSPageBarView ()<UIScrollViewDelegate>

@property(weak, nonatomic) UIColor * normalColor;
@property(weak, nonatomic) UIFont * normalFont;

@property(weak, nonatomic) UIColor * selectedColor;
@property(weak, nonatomic) UIFont * selectedFont;

@property(weak, nonatomic) UIView * selectedBottomLine;

@property(strong, nonatomic) NSArray * itemTitles;

@property(assign, nonatomic) CGSize cellSize;

@property(assign, nonatomic) NSInteger selectedCellIndex;

@property(strong, nonatomic) UIScrollView * pageBarScrollView;

@end

@implementation ZSPageBarView

- (instancetype)initNormalColor:(UIColor *)normalColor normalFont:(UIFont *)normalFont selectedColor:(UIColor *)selectedColor selectedFont:(UIFont *)selectedFont itemTitles:(NSArray *)itemTitles cellSize:(CGSize)cellSize  {
    
    if (self = [super init]) {
        self.normalColor = normalColor;
        self.normalFont = normalFont;
        self.selectedColor = selectedColor;
        self.selectedFont = selectedFont;
        self.itemTitles = itemTitles;
        self.cellSize = cellSize;
        
        [self setupUI];
    }
    return self;
}



- (void)setupUI {
    UIScrollView * pageBarScrollView = [[UIScrollView alloc] init];
    [self addSubview:pageBarScrollView];
    self.pageBarScrollView = pageBarScrollView;
    self.pageBarScrollView.contentSize = CGSizeMake(self.cellSize.width * self.itemTitles.count, self.cellSize.height);
    self.pageBarScrollView.delegate = self;
    self.pageBarScrollView.showsHorizontalScrollIndicator = NO;
    self.pageBarScrollView.showsVerticalScrollIndicator = NO;
    [self.pageBarScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self);
    }];
    
    
    for (int i = 0; i < self.itemTitles.count; i++) {
        UIButton * button = [[UIButton alloc] init];
        button.tag = TagAdditional + i;
        [button setTitle:self.itemTitles[i] forState:UIControlStateNormal];
        
        [button setTitleColor:self.selectedColor forState:UIControlStateSelected];
        [button setTitleColor:self.normalColor forState:UIControlStateNormal];
        if (i == 0) {
            button.selected = YES;
            [button.titleLabel setFont:self.selectedFont];
        } else {
            [button.titleLabel setFont:self.normalFont];
        }
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.pageBarScrollView addSubview:button];
        button.frame = CGRectMake(self.cellSize.width * i, 0, self.cellSize.width, self.cellSize.height - 1);

    }
    
    for (int i = 0; i < self.itemTitles.count - 1; i++) {
        UIView * midLine = [[UIView alloc] init];
        [self.pageBarScrollView addSubview:midLine];
        midLine.backgroundColor = [UIColor colorWithRed:229.0/256.0 green:229.0/256.0 blue:229.0/256.0 alpha:1];
        CGFloat space = 10;
        midLine.frame = CGRectMake(self.cellSize.width * (i + 1), space, 1, self.cellSize.height - 2 * space);
    }
    
    UIView * selectedBottomLine = [[UIView alloc] init];
    self.selectedBottomLine = selectedBottomLine;
    [self.pageBarScrollView addSubview:selectedBottomLine];
    self.selectedBottomLine.backgroundColor = self.selectedColor;
    self.selectedBottomLine.frame = CGRectMake(0, self.cellSize.height-1, self.cellSize.width, 1);
    
}


- (void)buttonAction:(UIButton *)sender {
    [self selectedItemWithIndex:sender.tag - TagAdditional];
}


- (void)selectedItemWithIndex:(NSInteger)index {
    [self layoutIfNeeded];
    //取消上一个选中的
    UIButton * lastbtn = [self viewWithTag:self.selectedCellIndex + TagAdditional];
    lastbtn.selected = NO;
    lastbtn.titleLabel.font = self.normalFont;
    
    UIButton * btn = [self viewWithTag:index + TagAdditional];
    btn.selected = YES;
    btn.titleLabel.font = self.selectedFont;


    if (btn == nil) {
        return;
    }
    [UIView animateWithDuration:0.25 animations:^{
        
        NSInteger tagSub = btn.tag - self.selectedCellIndex - TagAdditional;
        //线移动
        self.selectedBottomLine.frame = CGRectMake(btn.frame.origin.x, self.cellSize.height - 1, self.cellSize.width, 1);
        if (btn.tag == TagAdditional) {
            self.pageBarScrollView.contentOffset = CGPointMake(0, 0);
        } else if (btn.tag == (TagAdditional + self.itemTitles.count-1)) {
            if (self.pageBarScrollView.contentSize.width > [UIScreen mainScreen].bounds.size.width) {
                self.pageBarScrollView.contentOffset = CGPointMake(btn.bounds.size.width * self.itemTitles.count - [UIScreen mainScreen].bounds.size.width, 0);
            }
        } else {
            [self layoutIfNeeded];
            CGRect rect = [btn convertRect:btn.bounds toView:[[[UIApplication sharedApplication] delegate] window]];
            if (tagSub >= 1) {
                //向右
                if (rect.origin.x >= ([UIScreen mainScreen].bounds.size.width - self.cellSize.width -1.5) && rect.origin.x <= ([UIScreen mainScreen].bounds.size.width - self.cellSize.width +1.5)) {
                    self.pageBarScrollView.contentOffset = CGPointMake(self.pageBarScrollView.contentOffset.x + self.cellSize.width, 0);
                }
            } else if (tagSub == -1) {
                //向左
                if (rect.origin.x < self.cellSize.width) {
                    CGFloat temp = self.pageBarScrollView.contentOffset.x  - self.cellSize.width;
                    if (temp < 0) {
                        temp = 0;
                    }
                    self.pageBarScrollView.contentOffset = CGPointMake(temp, 0);
                }
            }
            
        }
    } completion:^(BOOL finished) {
        
    }];

    self.selectedCellIndex = index;
}





@end
